package com.synjones.ui.check;

import com.synjones.AppExecutors;
import com.synjones.Constant;
import com.synjones.entry.SignPerson;
import com.synjones.network.Api;
import com.synjones.ui.FirstScreenRouter;

public class CheckPresenter implements CheckContract.Presenter {

    private CheckContract.View view;
    private FirstScreenRouter main;
    private SignPerson signPerson;

    public CheckPresenter(CheckContract.View view, FirstScreenRouter main, SignPerson signPerson) {
        this.view = view;
        this.main = main;
        this.signPerson = signPerson;
    }

    @Override
    public void Sign(final String invitationCode, final String idno) {
        AppExecutors.ioExecutor.execute(new Runnable() {
            @Override
            public void run() {
                final String code = Api.sign(invitationCode, "", "1", "", "",idno);
                AppExecutors.mainThread.execute(new Runnable() {
                    @Override
                    public void run() {
                        switch (code){
                            case "500":
                                view.showBackInfo("服务器错误", Constant.Color.COLOR_FAIL);
                                break;
                            case "600":
                                view.showBackInfo("网络错误", Constant.Color.COLOR_FAIL);
                                break;
                            default:
                                if (code.equals("signed"))  {
                                    view.showBackInfo("重复签到", Constant.Color.COLOR_WARNNING);
                                }
                                else if (code.equals("pushFail"))
                                    view.showBackInfo("同步失败", Constant.Color.COLOR_SUCCESS);
                                else {
                                    view.showBackInfo("签到成功", Constant.Color.COLOR_SUCCESS);
                                    AppExecutors.ioExecutor.execute(new Runnable() {
                                        @Override
                                        public void run() {
                                            main.printBill(signPerson);
                                        }
                                    });
                                }
                                break;
                        }

                    }
                });
            }
        });

    }
}
